package com.zhuge_new.Activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhuge_new.Base.BaseActivity;
import com.zhuge_new.Fragment.HomeFragment;
import com.zhuge_new.Fragment.MinFragment;
import com.zhuge_new.Fragment.RedFragment;
import com.zhuge_new.R;

public class MainActivity extends BaseActivity {

    private TextView home_tab1;
    private TextView home_tab2;
    private TextView home_tab3;
    private LinearLayout home_subject;
    private HomeFragment homeFragment;
    private RedFragment redFragment;
    private MinFragment minFragment;
    private Fragment fragment;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        inittitle("首页");
    }

    @Override
    protected void inittitle(String msg) {
        super.inittitle(msg);
        home_tab1 = findViewById(R.id.home_tab1);
        home_tab2 = findViewById(R.id.home_tab2);
        home_tab3 = findViewById(R.id.home_tab3);
        home_subject = findViewById(R.id.home_subject);
        home_tab1.setOnClickListener(this);
        home_tab2.setOnClickListener(this);
        home_tab3.setOnClickListener(this);

        homeFragment = new HomeFragment();
        redFragment = new RedFragment();
        minFragment = new MinFragment();
        fragmentManager = getSupportFragmentManager();

        FragmentTransaction transaction= fragmentManager.beginTransaction();
        transaction.add(R.id.home_context, homeFragment).commit();
        fragment = homeFragment;

        home_tab1.setSelected(true);

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.home_tab1:
                reset();
                home_tab1.setSelected(true);
                SwitchFragment(homeFragment);
                break;
            case R.id.home_tab2:
                reset();
                home_tab2.setSelected(true);
                SwitchFragment(redFragment);
                break;
            case R.id.home_tab3:
                reset();
                home_tab3.setSelected(true);
                SwitchFragment(minFragment);
                break;
        }
    }

    //重置选中状态
    private void reset() {
        home_tab1.setSelected(false);
        home_tab2.setSelected(false);
        home_tab3.setSelected(false);
    }

    //切换fragment
    private void SwitchFragment(Fragment tofragment) {

        if (fragment != tofragment) {

            FragmentTransaction transaction = fragmentManager.beginTransaction();

            if (!tofragment.isAdded()) {

                transaction.hide(fragment).add(R.id.home_context, tofragment).commit();

            } else {

                transaction.hide(fragment).show(tofragment).commit();
            }

            fragment = tofragment;
        }

    }
}
